package com.yunhe.inspect.util;

import com.yunhe.common.constant.SystemConsts;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: DongWenYu
 * @Date: 2021/5/8 16:24
 */
@Component
public class MyHeaderUtil {

    public Long getHeaderUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIdStr = request.getHeader(SystemConsts.HEADER.USER_ID.value());
        if (StringUtils.isEmpty(userIdStr)) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return userId;
    }

    public Long getHeaderStation() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String stationIdStr = request.getHeader(SystemConsts.HEADER.STATION_ID.value());
        if (StringUtils.isEmpty(stationIdStr)) {
            return null;
        }
        Long stationId = Long.parseLong(stationIdStr);
        return stationId;
    }

    public Long getHeaderOrg(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String orgIdStr = request.getHeader(SystemConsts.HEADER.ORG_ID.value());
        if (StringUtils.isEmpty(orgIdStr)) {
            return null;
        }
        Long orgId = Long.parseLong(orgIdStr);
        return orgId;
    }
}
